package com.example.ccj.b.c.custom;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.u.AdaptScreenUtils;
import com.example.ccj.u.RegexUtils;
import com.example.ccj.u.text.TextInputType;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 自定义输入框
 */
public class MEditTextView extends androidx.appcompat.widget.AppCompatEditText implements View.OnClickListener {
    public static final int ALL = 0;
    public static final int NUMBER = 1;
    public static final int PHONE = 2;
    public static final int DOUBLE = 3;

    @IntDef({ALL,NUMBER, PHONE, DOUBLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface EditTextInputType {}


    public MEditTextView(@NonNull Context context) {
        super(context);
        super.setOnClickListener(this);
        setBackground(getResources().getDrawable(R.drawable.edit_text_def2));
    }

    public MEditTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MEditTextView);
        //输入类型
        int inputType = typedArray.getInteger(R.styleable.MEditTextView_m_input_type,0);
        int maxLength = typedArray.getInteger(R.styleable.MEditTextView_m_max_length,255);
        int littleMaxLength = typedArray.getInteger(R.styleable.MEditTextView_m_double_little_max_length,2);
        handlerInputType(inputType,maxLength,littleMaxLength);
        setBackground(getResources().getDrawable(R.drawable.edit_text_def2));
        super.setOnClickListener(this);
        typedArray.recycle();
    }

    public void setPaddingForDp(int left, int top, int right, int bottom) {
        setPadding(AdaptScreenUtils.pt2Px(left),
                AdaptScreenUtils.pt2Px(top),
                AdaptScreenUtils.pt2Px(right),
                AdaptScreenUtils.pt2Px(bottom));
    }

    ColorStateList oldColor;

    private void handlerInputType(@EditTextInputType int inputType,int maxLength,int littleMaxLength){
        if (inputType == ALL) return;
        if (inputType == NUMBER) TextInputType.setEditTextInPutTypeToInt(this,maxLength);
        if (inputType == PHONE) TextInputType.setEditTextInPutTypeToPhone(this);
        if (inputType == DOUBLE) TextInputType.setEditTextInPutTypeToDouble(this,maxLength,littleMaxLength);
        oldColor = getTextColors();

        if (inputType == PHONE) {
            setOnFocusChangeListener((v, hasFocus) -> {
                if (!hasFocus) {
                    if (getText() != null) {
                        String string = getText().toString().trim();
                        if (!RegexUtils.isMobileExact(string)) {
                            setTextColor(Color.RED);
                        } else {
                            setTextColor(oldColor);
                        }
                    }
                }
            });
        }
    }

    public void setInputType(@EditTextInputType int inputType,int maxLength,int littleMaxLength) {
        handlerInputType(inputType,maxLength,littleMaxLength);
    }

    long clickTime = 0;

    int checkInterval;
    boolean onlyOne;
    OnClickListener onClickListenerN;


    public void setOnClickListener(@Nullable OnClickListener onClickListenerN) {
        this.onClickListenerN = onClickListenerN;
    }

    @Override
    public void onClick(View v) {
        if (onlyOne) {
            if (onClickListenerN != null) onClickListenerN.onClick(v);
            this.setEnabled(false);
        } else if (!(System.currentTimeMillis() - clickTime < checkInterval)) {
            clickTime = System.currentTimeMillis();
            if (onClickListenerN != null) onClickListenerN.onClick(v);
        }
    }


    public int getCheckInterval() {
        return checkInterval;
    }

    public void setCheckInterval(int checkInterval) {
        this.checkInterval = checkInterval;
    }

    public boolean isOnlyOne() {
        return onlyOne;
    }

    public void setOnlyOne(boolean onlyOne) {
        this.onlyOne = onlyOne;
    }

}
